package com.example.john.contentprovederdemo;

import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }



    public void getAllContacts(View view) {
       Cursor cursor= getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()){
            String contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String displayName=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


            Log.i("john",contactId+"............"+displayName);

        }

    }

    public void getAllPhonesById(View view) {
        //假设联系人的id=2
        String id="2";
        String queryString=ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+id;
        String queryString1=ContactsContract.CommonDataKinds.Email.CONTACT_ID+"="+id;
        Cursor cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,queryString,null,null);
        while (cursor.moveToNext()){
            String phone=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            Log.i("john","id=2's phoneNumber is "+phone+".........");
        }


    }

    public void getAllEmailsById(View view) {
        //假设联系人的id=2
        String id="2";

        String queryString=ContactsContract.CommonDataKinds.Email.CONTACT_ID+"="+id;
        Cursor cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,queryString,null,null);
        while (cursor.moveToNext()){
            String email=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            Log.i("john","id=2's Email is "+email+".........");
        }


    }


}
